package xyz.penghaonan.p2plib.helper.connect;

import com.penghaonan.appframework.AppDelegate;
import com.penghaonan.appframework.utils.WeakObservableList;

import xyz.penghaonan.p2plib.helper.message.LocationStartTrackRequestMsg;
import xyz.penghaonan.p2plib.helper.message.MsgSender;
import xyz.penghaonan.p2plib.job.base.JobListenerAdapter;
import xyz.penghaonan.p2plib.job.jobs.StartClientJob;

public class ClientConnectHelper {

    public static final int STATE_DISCONNECT = 0;
    public static final int STATE_CONNECTING = 1;
    public static final int STATE_CONNECT_FAILED = 2;
    public static final int STATE_CONNECTED = 3;

    private int state = STATE_DISCONNECT;

    private static final WeakObservableList<OnConnectStateChangedListener> connectStateChangedListenerList = new WeakObservableList<>();

    public interface OnConnectStateChangedListener {
        void onConnectStateChanged(int state);

        void onConnectProgress(float progress, String msg);

        void onConnectFinished(boolean success, String msg);
    }

    public void addOnConnectStateChangedListener(OnConnectStateChangedListener listener) {
        connectStateChangedListenerList.addListener(listener);
    }

    public void removeOnConnectStateChangedListener(OnConnectStateChangedListener listener) {
        connectStateChangedListenerList.removeListener(listener);
    }

    public void onDisconnect() {
        setState(STATE_DISCONNECT);
    }

    private void setState(final int state) {
        if (this.state == state) {
            return;
        }
        this.state = state;
        AppDelegate.post(new Runnable() {
            @Override
            public void run() {
                connectStateChangedListenerList.iterate(new WeakObservableList.OnIterator<OnConnectStateChangedListener>() {
                    @Override
                    protected boolean onIterator(OnConnectStateChangedListener item) {
                        item.onConnectStateChanged(state);
                        return false;
                    }
                });
            }
        });
    }

    public int getState() {
        return state;
    }

    public boolean isConnected() {
        return getState() == STATE_CONNECTED;
    }

    public String getStateDesc() {
        switch (state) {
            case ClientConnectHelper.STATE_CONNECTED:
                return "已连接";
            case ClientConnectHelper.STATE_CONNECT_FAILED:
                return "连接失败";
            case ClientConnectHelper.STATE_CONNECTING:
                return "正在连接";
            case ClientConnectHelper.STATE_DISCONNECT:
                return "未连接";
        }
        return "";
    }

    public void connect() {
        if (getState() == STATE_CONNECTING || getState() == STATE_CONNECTED) {
            return;
        }
        MsgSender.initDevice(false);
        setState(STATE_CONNECTING);
        StartClientJob startClientJob = new StartClientJob();
        startClientJob.addListener(new JobListenerAdapter() {
            @Override
            public void onJobFinish(final boolean success, final String msg) {
                super.onJobFinish(success, msg);
                if (success) {
                    setState(STATE_CONNECTED);
                    connectStateChangedListenerList.iterate(new WeakObservableList.OnIterator<OnConnectStateChangedListener>() {
                        @Override
                        protected boolean onIterator(OnConnectStateChangedListener item) {
                            item.onConnectFinished(true, msg);
                            return false;
                        }
                    });
                } else {
                    setState(STATE_CONNECT_FAILED);
                    connectStateChangedListenerList.iterate(new WeakObservableList.OnIterator<OnConnectStateChangedListener>() {
                        @Override
                        protected boolean onIterator(OnConnectStateChangedListener item) {
                            item.onConnectFinished(false, msg);
                            return false;
                        }
                    });
                }
            }

            @Override
            public void onJobProgress(final float progress, final String msg) {
                super.onJobProgress(progress, msg);
                connectStateChangedListenerList.iterate(new WeakObservableList.OnIterator<OnConnectStateChangedListener>() {
                    @Override
                    protected boolean onIterator(OnConnectStateChangedListener item) {
                        item.onConnectProgress(progress, msg);
                        return false;
                    }
                });
            }
        });
        startClientJob.start();
    }
}
